package com.example.project.config;

import com.example.project.constant.Constants;
import com.example.project.intecetper.AuthLoginIntecepter;
import com.example.project.intecetper.TokenInterceptor;
import com.example.project.utils.FileMangerUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

import static com.example.project.constant.Constants.SYSTEM_NAME;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    SystemConfig systemConfig;

     @Resource
     private TokenInterceptor tokenInterceptor;

     @Resource
     private AuthLoginIntecepter authLoginIntecepter;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                // .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {                         // F:\2024peoject\data-management-system\data\data
        registry.addResourceHandler("/files/"+"**").addResourceLocations("file:" + Constants.PROJECT_ROOT_DIRECTORY+systemConfig.getSystemName()+"/" );
//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");


       //静态资源
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/").resourceChain(true);
        registry.addResourceHandler("/json/**").addResourceLocations("classpath:/static/json/").resourceChain(true);
        registry.addResourceHandler("/tinymce/**").addResourceLocations("classpath:/tinymce/assets/").resourceChain(true);
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/favicon.ico").resourceChain(true);
        registry.addResourceHandler("/logo.png").addResourceLocations("classpath:/logo.png").resourceChain(true);
    }
//
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(tokenInterceptor);
         registry.addInterceptor(authLoginIntecepter)
                 .excludePathPatterns("/code/*")
                 .excludePathPatterns("/**/*.html")
                 .excludePathPatterns("/front")
                 .excludePathPatterns("/favicon.ico");
        WebMvcConfigurer.super.addInterceptors(registry);
    }


}
